package com.jerry.springcloud.service.impl;

import com.github.pagehelper.PageHelper;
import com.jerry.springcloud.bean.Product;
import com.jerry.springcloud.dto.ProductDto;
import com.jerry.springcloud.enums.ProductStatusEnum;
import com.jerry.springcloud.enums.ResultEnum;
import com.jerry.springcloud.exception.SellerException;
import com.jerry.springcloud.mapper.ProductMapper;
import com.jerry.springcloud.service.ProductService;
import com.jerry.springcloud.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public Product findOne(String productId) {
        return productMapper.getProductById(productId);
    }

    @Override
    public List<Product> findUpAll() {
        return productMapper.getProductListByStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public List<Product> findAll() {
        return productMapper.getProductList();
    }

    @Override
    public List<Product> findAllByPage(Integer page, Integer pageSize) {
        // 开始分页,  *****只用加入这一行，就实现了分页*****
        PageHelper.startPage(page, pageSize);

        return productMapper.getProductList();
    }

    @Override
    public List<Product> findByIdIn(List<Integer> idList) {
        return productMapper.getProductListByIdIn(idList);
    }

    @Override
    public Product saveOrUpdate(Product product) {
        //如果productId为空, 说明是新增
        if (StringUtils.isEmpty(product.getId())) {
            product.setId(KeyUtil.genUniqueKey());
            if (productMapper.insertProduct(product) == 1) {
                return product;
            }
        } else {
            Product updateProduct = findOne(product.getId());
            BeanUtils.copyProperties(product, updateProduct);
            if (productMapper.updateProduct(updateProduct) == 1) {
                return updateProduct;
            }
        }
        return null;
    }

    @Override
    public Product onSale(String productId) {
        Product product = productMapper.getProductById(productId);
        if (product == null) {
            throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (product.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellerException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        product.setStatus(ProductStatusEnum.UP.getCode());
        if (productMapper.updateProduct(product) == 1) {
            return product;
        }
        return null;
    }

    @Override
    public Product offSale(String productId) {
        Product product = productMapper.getProductById(productId);
        if (product == null) {
            throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if (product.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellerException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        product.setStatus(ProductStatusEnum.DOWN.getCode());
        if (productMapper.updateProduct(product) == 1) {
            return product;
        }
        return null;
    }

    @Override
    @Transactional
    public void increaseStock(List<ProductDto> productDtoList) {
        for (ProductDto productDto: productDtoList) {
            Product product = productMapper.getProductById(productDto.getId());
            if (product == null) {
                throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = product.getStock() + productDto.getQuantity();
            product.setStock(result);

            productMapper.updateProduct(product);
        }

    }

    @Override
    @Transactional
    public void decreaseStock(List<ProductDto> productDtoList) {
        for (ProductDto productDto: productDtoList) {
            Product product = productMapper.getProductById(productDto.getId());
            if (product == null) {
                throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = product.getStock() - productDto.getQuantity();
            if (result < 0) {
                throw new SellerException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            product.setStock(result);

            productMapper.updateProduct(product);
        }
    }

}
